package com.triones.yjj.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.StringUtils;
import com.triones.yjj.entity.GroupPurchaseGoodsItemDO;
import com.triones.yjj.entity.MemberGroupPurchaseDO;
import com.triones.yjj.entity.MemberGroupPurchaseItemDO;
import com.triones.yjj.entity.bo.MemberGroupPurchaseBO;
import com.triones.yjj.entity.vo.MemberGroupPurchaseVO;
import com.triones.yjj.entity.vo.WxGroupPurchaseVO;
import com.triones.yjj.entity.vo.WxMemberGroupPurchaseVO;
import com.triones.yjj.mapper.GroupPurchaseGoodsItemMapper;
import com.triones.yjj.mapper.GroupPurchaseGoodsMapper;
import com.triones.yjj.mapper.MemberGroupPurchaseItemMapper;
import com.triones.yjj.mapper.MemberGroupPurchaseMapper;
import com.triones.yjj.service.MemberGroupPurchaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 活动管理 service 业务实现
 *
 * @Author: triones
 */
@Service
@RequiredArgsConstructor
public class MemberGroupPurchaseImpl implements MemberGroupPurchaseService {

    private final MemberGroupPurchaseMapper memberGroupPurchaseMapper;

    private final GroupPurchaseGoodsItemMapper groupPurchaseGoodsItemMapper;

    private final MemberGroupPurchaseItemMapper memberGroupPurchaseItemMapper;



    @Override
    public MemberGroupPurchaseVO selectVoById(Long id) {
        return memberGroupPurchaseMapper.selectVoById(id);
    }


    public Long createMemberGroupPurchase(MemberGroupPurchaseBO memberGroupPurchaseBO) {

        MemberGroupPurchaseDO memberGroupPurchaseDO = new MemberGroupPurchaseDO();
        memberGroupPurchaseDO.setMemberId(memberGroupPurchaseBO.getMemberId());
        memberGroupPurchaseDO.setMemberWxUserId(memberGroupPurchaseBO.getMemberWxUserId());
        memberGroupPurchaseDO.setTeamName(memberGroupPurchaseBO.getTeamName());
        memberGroupPurchaseDO.setPersonnelNumber(0);
        memberGroupPurchaseDO.setMaxPersonnelNumber(memberGroupPurchaseBO.getMaxPersonnelNumber());
        memberGroupPurchaseDO.setNature(memberGroupPurchaseBO.getNature());//团性质  阶梯=1为1 一节拼团 阶梯>1为1 多阶拼团
        memberGroupPurchaseDO.setActivityId(memberGroupPurchaseBO.getActivityId());
        memberGroupPurchaseDO.setGroupPurchaseGoodsId(memberGroupPurchaseBO.getGroupPurchaseGoodsId());
        memberGroupPurchaseDO.setGroupPurchaseGoodsItemId(memberGroupPurchaseBO.getGroupPurchaseGoodsItemId());
        memberGroupPurchaseDO.setNextGroupPurchaseGoodsItemId(memberGroupPurchaseBO.getGroupPurchaseGoodsItemId());
        //获取下一个阶梯
        GroupPurchaseGoodsItemDO goodsItemDO = getNextGoodsItem(memberGroupPurchaseBO.getGroupPurchaseGoodsId(),memberGroupPurchaseBO.getMaxPersonnelNumber());
       if(ObjectUtil.isNotEmpty(goodsItemDO)){
           memberGroupPurchaseDO.setNextGroupPurchaseGoodsItemId(goodsItemDO.getId());
       }
       //status 0满团 1待成团 2成团 3满团
       if(memberGroupPurchaseBO.getMaxPersonnelNumber()>1){
           memberGroupPurchaseDO.setStatus(1);
       }else{
           if(memberGroupPurchaseBO.getNature().intValue()==1){
               memberGroupPurchaseDO.setStatus(3);
           }else{
               memberGroupPurchaseDO.setStatus(1);
           }
       }
        memberGroupPurchaseDO.setUserId(memberGroupPurchaseBO.getUserId());
        memberGroupPurchaseMapper.insert(memberGroupPurchaseDO);
        return memberGroupPurchaseDO.getId();
    }

    /**
     * 查询
     * @param activityId
     * @param pageQuery
     * @return
     */
    public TableDataInfo<WxMemberGroupPurchaseVO> selectPageList(Long activityId, PageQuery pageQuery) {
        QueryWrapper<MemberGroupPurchaseDO> wrapper = new QueryWrapper<>();
        // 默认排序 时间倒叙
        if (StringUtils.isBlank(pageQuery.getOrderByColumn()) || StringUtils.isBlank(pageQuery.getIsAsc())) {
            wrapper.orderByDesc("g.create_time");
        }
        wrapper.notIn("g.`status`",3,1,-1);//满团、待成团（未付款）、已作废
        wrapper.eq("g.activity_id", activityId);
        Page<WxMemberGroupPurchaseVO> result = memberGroupPurchaseMapper.selectPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    private GroupPurchaseGoodsItemDO getNextGoodsItem(Long groupPurchaseGoodsId,int constituteNumber){
        QueryWrapper<GroupPurchaseGoodsItemDO> wrapper = new QueryWrapper<>();
        wrapper.eq("group_purchase_goods_id",groupPurchaseGoodsId);
        wrapper.gt("constitute_number",constituteNumber);
        wrapper.last(" LIMIT 1");
        wrapper.orderByAsc("constitute_number");
        return  groupPurchaseGoodsItemMapper.selectOne(wrapper);
    }

    /**
     * 查
     * @param id
     * @return
     */
    public Map getMemberGroupPurchaseAndItem(Long id) {
        Map map = new HashMap();
        WxGroupPurchaseVO memberGroupPurchaseVO = memberGroupPurchaseMapper.getGroupPurchaseById(id);
        if(ObjectUtil.isEmpty(memberGroupPurchaseVO)){
            throw new ServiceException("团不存在！");
        }
        //查询团最高福利商品
        GroupPurchaseGoodsItemDO groupPurchaseGoodsItemDO = getMaxGoodsItem(memberGroupPurchaseVO.getGroupPurchaseGoodsId());
        if(ObjectUtil.isNotEmpty(groupPurchaseGoodsItemDO)){
            memberGroupPurchaseVO.setMaxWelfareName(groupPurchaseGoodsItemDO.getWelfareName());
        }
        map.put("groupPurchase",memberGroupPurchaseVO);
//        map.put("groupPurchaseItem",memberGroupPurchaseItemMapper.getItemList(id));
        map.put("groupPurchaseItem",memberGroupPurchaseItemMapper.apiGetItemList(id));
        return map;
    }

    /**
     * 查询客户活动参团信息
     * @param memberId
     * @return
     */
    public Map getMemberGroupPurchaseAndItemInfo(Long activityId,Long memberId) {
        Map map = new HashMap();
        WxGroupPurchaseVO memberGroupPurchaseVO = memberGroupPurchaseMapper.getMemberGroupPurchaseDetail(activityId,memberId);
        if(ObjectUtil.isEmpty(memberGroupPurchaseVO)){
            throw new ServiceException("团不存在！");
        }
        //查询团最高福利商品
        GroupPurchaseGoodsItemDO groupPurchaseGoodsItemDO = getMaxGoodsItem(memberGroupPurchaseVO.getGroupPurchaseGoodsId());
        if(ObjectUtil.isNotEmpty(groupPurchaseGoodsItemDO)){
            memberGroupPurchaseVO.setMaxWelfareName(groupPurchaseGoodsItemDO.getWelfareName());
        }
        map.put("groupPurchase",memberGroupPurchaseVO);
        map.put("groupPurchaseItem",memberGroupPurchaseItemMapper.apiGetItemList(memberGroupPurchaseVO.getId()));
        return map;
    }

    private GroupPurchaseGoodsItemDO getMaxGoodsItem(Long groupPurchaseGoodsId){
        QueryWrapper<GroupPurchaseGoodsItemDO> wrapper = new QueryWrapper<>();
        wrapper.eq("group_purchase_goods_id",groupPurchaseGoodsId);
        wrapper.last(" LIMIT 1");
        wrapper.orderByDesc("constitute_number");
        return  groupPurchaseGoodsItemMapper.selectOne(wrapper);
    }
}
